package com.baylor.server.business.common;

import com.baylor.server.business.constant.GeneralErrorEnum;
import com.baylor.server.business.exception.BusinessException;
import com.baylor.server.business.exception.DBException;
import com.baylor.server.business.exception.ValidationException;
import com.baylor.server.business.util.Lists;
import com.baylor.server.business.util.Maps;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

public abstract class BaseController {

    private static final Logger LOGGER = Logger.getLogger(BaseController.class);
    
    private static final Map<String, String> EMPTY_DATA = Maps.newHashMap();
    
    protected @Value("project.name")
    String projectName;
    
    protected @Value("${filerepo.upload.path}")
    String uploadPath;
    
    protected @Value("${base.url}")
    String baseUrl;

    protected boolean ReadResultData(ResultModel resultvo, ResponseObject response) {
        if (resultvo.getResults() != null && resultvo.getResults().size() > 0) {
            response.addData(resultvo.getResults());
        } else if (resultvo.getMapResults() != null && resultvo.getMapResults().size() > 0) {
            response.addData(resultvo.getMapResults());
        } else if (resultvo.getEmptyListResults() != null && resultvo.getEmptyListResults().size() > 0) {
            response.addData(Lists.newArrayList());
        } else {
            response.addData(EMPTY_DATA);
        }
        response.addInfo("page", resultvo.getPage());

        return true;
    }

    @ExceptionHandler(Exception.class)
    public @ResponseBody
    ResponseObject handleUncaughtException(Exception ex) {
        LOGGER.error(ex.getMessage(), ex.getCause());

        ResponseObject response = new ResponseObject();
        response.setCommand("系统异常");
        response.setSuccess(false);
        response.setErrMsg(GeneralErrorEnum.ERR_SYSTEM_RUNNING.getErrorCode(), ex.getMessage());
        response.addData(EMPTY_DATA);

        return response;
    }

    @ExceptionHandler(ValidationException.class)
    public @ResponseBody
    ResponseObject handleValidationException(ValidationException validationEx) {
        LOGGER.error(validationEx.getMessage(), validationEx.getCause());

        ResponseObject errResponse = null;
        if (validationEx.getResponse() == null) {
            errResponse = new ResponseObject();
        }
        errResponse.setCommand("数据校验异常");
        errResponse.setSuccess(false);
        errResponse.setErrMsg(validationEx.getErrorCode(), validationEx.getMessage());
        errResponse.addData(EMPTY_DATA);

        return errResponse;
    }

    @ExceptionHandler(BusinessException.class)
    public @ResponseBody
    ResponseObject handleBusinessException(BusinessException businessEx) {
        LOGGER.error(businessEx.getMessage(), businessEx.getCause());
        
        ResponseObject errResponse = null;
        if (businessEx.getResponse() == null) {
            errResponse = new ResponseObject();
        }
        errResponse.setCommand("业务逻辑异常");
        errResponse.setSuccess(false);
        errResponse.setErrMsg(businessEx.getErrorCode(), businessEx.getMessage());
        errResponse.addData(EMPTY_DATA);

        return errResponse;
    }

    @ExceptionHandler(DBException.class)
    public @ResponseBody
    ResponseObject handleValidationException(DBException dbEx) {
        LOGGER.error(dbEx.getMessage(), dbEx.getCause());

        ResponseObject errResponse = null;
        if (dbEx.getResponse() == null) {
            errResponse = new ResponseObject();
        }
        errResponse.setCommand("数据库操作异常");
        errResponse.setSuccess(false);
        errResponse.setErrMsg(dbEx.getErrorCode(), dbEx.getMessage());
        errResponse.addData(EMPTY_DATA);

        return errResponse;
    }

    @ExceptionHandler(HttpMessageNotWritableException.class)
    public @ResponseBody
    ResponseObject handleJSONConvertException(HttpMessageNotWritableException jsonEx) {
        LOGGER.error(jsonEx.getMessage(), jsonEx.getCause());

        ResponseObject errResponse = new ResponseObject();
        errResponse.setCommand("JSON格式转换异常");
        errResponse.setSuccess(false);
        errResponse.setErrMsg(GeneralErrorEnum.ERR_JSON_FORMAT.getErrorCode(),
                GeneralErrorEnum.ERR_JSON_FORMAT.getErrorMsg());
        errResponse.addData(EMPTY_DATA);

        return errResponse;
    }
}
